package hlhj.fhp.checkpoint.atys

import android.app.DatePickerDialog
import android.content.Intent
import android.view.Gravity
import android.view.View
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.customView.AndroidBug5497Workaround
import hlhj.fhp.checkpoint.customView.ChooseYearLocationPop
import hlhj.fhp.checkpoint.customView.ProcessPop
import hlhj.fhp.checkpoint.iview.CarCheckView
import hlhj.fhp.checkpoint.javaBean.AddYearBean
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import hlhj.fhp.checkpoint.javaBean.YearLimitBean
import hlhj.fhp.checkpoint.presenter.CarCheckPresenter
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_car_check.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Never Fear   on 2018\11\8 0008.
Never More....
 */
class CarCheckAty:BaseAty<CarCheckView,CarCheckPresenter>(), CarCheckView, ChooseYearLocationPop.OnYearLocClick {
    private var mYear: Int=0
    private var mMonth: Int=0
    private var mDay: Int=0

    private var processPop:ProcessPop?=null
    private var chooseLocPop:ChooseYearLocationPop?=null
    private val GOTOPAY: Int=1226

    override fun addYearResp(bean: AddYearBean) {
        try {
            ToastUtils.toast("订单提交成功，前去支付",this)
            val intent = Intent(this,PayAty::class.java)
            intent.putExtra("id",bean.info.id.toString())
            intent.putExtra("money",tvMoney.text.toString())
            intent.putExtra("orderType",1)
            startActivityForResult(intent,GOTOPAY)
            finish()
        }catch (e:Exception){
            e.printStackTrace()
        }

    }

    override fun initView() {
        AndroidBug5497Workaround.assistActivity(this)
        tvTittle.text="车辆年检"
        btRightText.setText("代办流程")
        btRightText.visibility=View.VISIBLE
        /*初始化年月份*/
        val ca = Calendar.getInstance()
        mYear = ca.get(Calendar.YEAR)
        mMonth = ca.get(Calendar.MONTH)
        mDay = ca.get(Calendar.DAY_OF_MONTH)

        processPop= ProcessPop(this)
        chooseLocPop= ChooseYearLocationPop(this)
    }

    override fun logicStart() {
    }

    override fun setListener() {
        btGoFee.setOnClickListener {

            if (btChoose.text.isEmpty()){
                ToastUtils.toast("请选择车牌所属",this)
                return@setOnClickListener
            }
            if (etNum.text.isEmpty()){
                ToastUtils.toast("请填写车牌",this)
                return@setOnClickListener
            }
            if (etVin.text.isEmpty()){
                ToastUtils.toast("请填写车身架号",this)
                return@setOnClickListener
            }
            if (etEngin.text.isEmpty()){
                ToastUtils.toast("请填写车身架号",this)
                return@setOnClickListener
            }
            if (btChooseTime.text.toString().equals("请选择年审日期")){
                ToastUtils.toast("请选择日期",this)
                return@setOnClickListener
            }
            if (etName.text.isEmpty()){
                ToastUtils.toast("请输入姓名",this)
                return@setOnClickListener
            }
            if (!isOk.isChecked){
                ToastUtils.toast("您未同意用户协议",this)
                return@setOnClickListener
            }
            presenter?.addYear("${btChoose.text.toString()}${etNum.text.toString()}",etVin.text.toString(),
                    etEngin.text.toString(),btChooseTime.text.toString(),etName.text.toString(),tvMoney.text.toString(),this)
        }
        btChoose.setOnClickListener {
            presenter?.getYearLimit(this)
        }
        chooseLocPop?.listener=this
        chooseLocPop?.setOnDismissListener {
            val params = window.attributes
            params.alpha=1f
            window.attributes=params
        }
        processPop?.setOnDismissListener {
            val params = window.attributes
            params.alpha=1f
            window.attributes=params
        }
        btRightText.setOnClickListener {
            processPop?.showAtLocation(father,Gravity.CENTER,0,0)
            val params = window.attributes
            params.alpha=0.5f
            window.attributes=params
        }
        btChooseTime.setOnClickListener {
            DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val days:String
                if (month + 1 < 10) {
                    if (dayOfMonth < 10) {
                        days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                    } else {
                        days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                    }
                } else {
                    if (dayOfMonth < 10) {
                        days = StringBuffer().append(year).append("-").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                    } else {
                        days = StringBuffer().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                    }

                }
                btChooseTime.text = days
            },mYear,mMonth,mDay).show()
        }
    }

    override fun click(loc: String, money: String) {
        btChoose.text=loc
        tvMoney.text=money
        chooseLocPop?.dismiss()
    }

    override fun getYearLoc(yearLimitBean: YearLimitBean) {
        chooseLocPop?.showAtLocation(father,Gravity.CENTER,0,0)

        val params = window.attributes
        params.alpha=0.5f
        window.attributes=params

        chooseLocPop?.refreshList(yearLimitBean.info as ArrayList<YearLimitBean.InfoBean>)
    }

    override fun bindView(): CarCheckView {
        return this
    }

    override fun createPresenter(): CarCheckPresenter {
        return CarCheckPresenter()
    }

    override val contentId: Int= R.layout.aty_car_check
}